/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.sys.web.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.web.BaseController;
import com.kaaction.modules.market.entity.Works;
import com.kaaction.modules.market.service.WorksService;
import com.kaaction.modules.operate.entity.Client;
import com.kaaction.modules.operate.service.ClientService;

/**
 * 上传Controller
 * @author zengbiao
 * @version 2017-08-07
 */
@Controller
@RequestMapping(value = "/api/v1/upload")
public class UserUploadApiController extends BaseController {
	
	@Autowired
	private WorksService worksService;
	
	@Autowired
	private ClientService clientService;

	/**
	 * Name of the directory where uploaded files will be saved, relative to
	 * the web application directory.
	 */
	private static final String SAVE_DIR = "uploadFiles";
	private boolean isMultipart;
	private int maxFileSize = 5 * 1024 * 1024;
	private int maxMemSize = 4 * 1024;
	private File file ;

	 
	public void fileOutputStream(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String userName = request.getParameter("username");
		String mobile = request.getParameter("usertel");
		String remarks = request.getParameter("kind");
		String imageLoc = request.getParameter("imageLoc");

		Client client = new Client();
		client.setName(userName);
		client.setMobile(mobile);
		clientService.save(client);

		Works work = new Works();
		work.setClient(client);
		work.setWorks(imageLoc);
		work.setOwnBy("1");
		work.setTitle("");
		work.setContent("");
		work.setRemarks(remarks);
		worksService.save(work);
		
		super.writeAjaxResponse(response, "success");
	  	         
	}

	/**
	 * handles file upload
	 */
	@RequestMapping(value = "/submit")
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		fileOutputStream(request,response);
	}

}